using AlibabaCloud.SDK.Dysmsapi20170525.Models;
using AlibabaCloud.TeaUtil.Models;
using Vin.Extension.Alibaba.Options;
using Vin.Extension.Alibaba.Tools;
using Vin.Tool.AspNetCore.Singleton;
using Vin.Tool.Core.JsonCategory;

namespace Vin.Extension.Alibaba.Services;

public class DysmsService : AlibabaBaseService<AlibabaCloud.SDK.Dysmsapi20170525.Client>
{
    public DysmsOption DysmsOption => VinApp.GetOption<DysmsOption>();

    public override string Endpoint => DysmsOption.Endpoint ?? "dysmsapi.aliyuncs.com";

    /// <summary>
    /// 发送短信
    /// </summary>
    /// <param name="phoneNumbers">接收短信的手机号码 支持向不同的手机号码发送短信，手机号码之间以半角逗号（,）分隔。上限为 1000 个手机号码。批量发送相对于单条发送，及时性稍有延迟。验证码类型的短信，建议单条发送。</param>
    /// <param name="templateCode">短信模板 Code。</param>
    /// <param name="templateParam">短信模板变量对应的实际值，请传入JSON 字符串。当您选择的模板内容含有变量时，此参数必填。参数个数应与模板内变量个数一致。</param>
    /// <param name="smsUpExtendCode">上行短信扩展码。上行短信指发送给通信服务提供商的短信，用于定制某种服务、完成查询，或是办理某种业务等，需要收费，按运营商普通短信资费进行扣费。</param>
    /// <param name="outId">外部流水扩展字段。</param>
    /// <param name="signName">短信签名名称。</param>
    /// <returns></returns>
    public async Task<(bool, SendSmsResponse)> SendSmsAsync(string phoneNumbers, string templateCode,
        object? templateParam = null, string? smsUpExtendCode = null, string? outId = null, string? signName = null)
    {
        var sendSmsRequest = new AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest
        {
            PhoneNumbers = phoneNumbers,
            SignName = signName ?? DysmsOption.SignName,
            TemplateCode = templateCode,
            TemplateParam = templateParam?.ToJson(),
            SmsUpExtendCode = smsUpExtendCode,
            OutId = outId
        };
        var res = await AccessClient.SendSmsWithOptionsAsync(sendSmsRequest, new RuntimeOptions());
        return (AlibabaTool.IsSuccess(res.Body.Code), res);
    }
}